<template>
  <a-modal
    title="添加業主"
    :width="600"
    class="model-yz"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
         <a-form-item v-show="false"><a-input  v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="業主名稱"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input placeholder="请输入業主名稱" v-decorator="['name', {rules: [{required: true, message: '请输入業主名稱！'}]}]" />
        </a-form-item>
        <a-form-item
          label="手機號"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input placeholder="请输入手機號" v-decorator="['phone', {rules: [{required: true, message: '请输入手機號！'}]}]" />
        </a-form-item>
        <a-form-item
          label="相冊"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <div class="clearfix">
            <a-upload
                    :customRequest="customRequest1"
                    :multiple="false"
                    :showUploadList="false"
                    :before-upload="beforeUpload"
                    name="file">
                    <a-button><a-icon type="upload"/>上傳正面照片</a-button>
                  </a-upload>
            <a-row>
              <a-col :span="4" v-for="(item, index) in fileList" :key="index" class="parImg">
                 <a-icon type="delete" class="parImg_icon" @click="delImg(item.fileId)"/>
                 <img :src="'/api/sysFileInfo/preview?id=' + item.fileId"  style="max-width: 99%;width:100px"/>
              </a-col>
            </a-row>
          </div>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { ownerLeaseEditYzXx } from '@/api/modular/main/ownerlease/ownerLeaseManage'
  import { sysFileInfoUpload } from '@/api/modular/system/fileManage'
  export default {
     components: {
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        memberLoading: false,
        form: this.$form.createForm(this),
        fileList: []
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        this.fileList = record.fileList
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              name: record.name,
              phone: record.phone
            }
          )
        }, 100)
      },
      beforeUpload(file) {
        const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
        if (!isJpgOrPng) {
          this.$message.error('只允許上傳圖片!')
        }
        const isLt2M = file.size / 1024 / 1024 < 6
        if (!isLt2M) {
          this.$message.error('圖片大小不允許超過6MB!')
        }
        return isJpgOrPng && isLt2M
      },
       /**
       * 上传文件
       */
      customRequest1 (data) {
        data.status = 'uploading'
        const formData = new FormData()
        formData.append('file', data.file)
        sysFileInfoUpload(formData).then((res) => {
          if (res.code === 200) {
            this.fileList.push({ fileId: res.data, url: '/api/sysFileInfo/preview?id=' + res.data })
          } else {
            this.$message.error('上传失败：' + res.msg)
          }
        })
      },
      delImg(id) {
        const newData = this.fileList.filter(item => item.fileId !== id)
        this.fileList = newData
      },
      disabledStartDate(startValue) {
        const endValue = this.endValue
        if (!startValue || !endValue) {
          return false
        }
        return startValue.valueOf() > endValue.valueOf()
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.files = this.fileList
            ownerLeaseEditYzXx(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', '')
                this.handleCancel()
              } else {
                this.$message.error(res.msg)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.fileList = []
      }
    }
  }
</script>
<style lang="less" scoped >
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
.parImg{
  position: relative;
}
.parImg_icon{
  position: absolute;
  top: 0;
  right: 0;
  color: rgba(255, 0, 0, 0.829);
}
.mocheck{
  background-color: rgba(58, 136, 180, 0.822);
  color: aliceblue;
}
</style>
